//
//  ModernLiteralsTest.m
//  test_simple_project
//
//  现代字面量语法特性测试实现 - 修复版本
//

#import "ModernLiteralsTest.h"

// 常量字面量实现
NSString * const kModernStringLiteral = @"Modern Objective-C String";
NSNumber * const kModernNumberLiteral = @42;
NSArray * const kModernArrayLiteral = @[@"item1", @"item2", @"item3"];
NSDictionary * const kModernDictionaryLiteral = @{@"key1": @"value1", @"key2": @"value2"};


@implementation ModernLiteralsTest

- (void)testStringLiterals {
    // 字符串字面量测试
    NSString *basicString = @"Hello, World!";
    NSString *multilineString = @"Line 1\n"
                               @"Line 2\n"
                               @"Line 3";
    NSString *unicodeString = @"Unicode: 你好世界! 🌍";
    NSString *escapedString = @"Escaped: \"quotes\" and \\backslashes\\";
    
    // 字符串字面量与格式化
    NSString *formattedString = [NSString stringWithFormat:@"Number: %@, String: %@", @123, @"test"];
    
    NSLog(@"String literals: %@, %@, %@, %@, %@", 
          basicString, multilineString, unicodeString, escapedString, formattedString);
}

- (void)testNumberLiterals {
    // 各种数字字面量测试
    NSNumber *integerLiteral = @42;
    NSNumber *negativeLiteral = @-17;
    NSNumber *floatLiteral = @3.14159f;
    NSNumber *doubleLiteral = @2.718281828;
    NSNumber *scientificLiteral = @1.23e-4;
    NSNumber *hexLiteral = @0xFF;
    NSNumber *octalLiteral = @0755;
    
    // 数字字面量表达式
    NSNumber *expressionLiteral = @(42 + 8 * 3);
    NSNumber *variableBoxing = @(integerLiteral.intValue * 2);
    
    NSLog(@"Number literals: %@, %@, %@, %@, %@, %@, %@, %@, %@", 
          integerLiteral, negativeLiteral, floatLiteral, doubleLiteral, 
          scientificLiteral, hexLiteral, octalLiteral, expressionLiteral, variableBoxing);
}

- (void)testBooleanLiterals {
    // 布尔字面量测试
    NSNumber *trueLiteral = @YES;
    NSNumber *falseLiteral = @NO;
    NSNumber *boolExpression = @(5 > 3);
    // 修复: 使用括号明确运算符优先级，避免 '&' within '||' 警告
    NSNumber *complexBoolExpression = @((YES && NO) || YES);
    
    // 布尔字面量在条件中的使用 - 修复: 避免隐式布尔转换
    if ([trueLiteral boolValue]) {
        NSLog(@"trueLiteral is true");
    }
    
    if (![falseLiteral boolValue]) {
        NSLog(@"falseLiteral is false");
    }
    
    NSLog(@"Boolean literals: %@, %@, %@, %@", 
          trueLiteral, falseLiteral, boolExpression, complexBoolExpression);
}

- (void)testArrayLiterals {
    // 数组字面量测试
    NSArray *emptyArray = @[];
    NSArray *simpleArray = @[@"apple", @"banana", @"cherry"];
    NSArray *mixedArray = @[@"string", @42, @YES, @3.14];
    NSArray *nestedArray = @[@"outer", @[@"inner1", @"inner2"], @"end"];
    
    // 数组字面量与变量
    NSString *fruit = @"orange";
    NSNumber *count = @5;
    NSArray *variableArray = @[fruit, count, @"literal"];
    
    // 数组字面量表达式
    NSArray *expressionArray = @[@(1 + 2), @(fruit.length), @(count.intValue * 2)];
    
    NSLog(@"Array literals: %@, %@, %@, %@, %@, %@", 
          emptyArray, simpleArray, mixedArray, nestedArray, variableArray, expressionArray);
}

- (void)testDictionaryLiterals {
    // 字典字面量测试
    NSDictionary *emptyDict = @{};
    NSDictionary *simpleDict = @{@"name": @"John", @"age": @25};
    NSDictionary *mixedDict = @{@"string": @"value", @"number": @42, @"bool": @YES};
    
    // 嵌套字典字面量
    NSDictionary *nestedDict = @{
        @"user": @{
            @"name": @"Alice",
            @"profile": @{
                @"age": @30,
                @"city": @"Beijing"
            }
        },
        @"settings": @{
            @"theme": @"dark",
            @"notifications": @YES
        }
    };
    
    // 字典字面量与变量
    NSString *key = @"dynamic_key";
    NSString *value = @"dynamic_value";
    NSDictionary *variableDict = @{key: value, @"static": @"static_value"};
    
    NSLog(@"Dictionary literals: %@, %@, %@, %@, %@", 
          emptyDict, simpleDict, mixedDict, nestedDict, variableDict);
}

- (void)testSubscriptSyntax {
    // 下标语法测试
    NSArray *array = @[@"zero", @"one", @"two", @"three"];
    NSDictionary *dict = @{@"first": @"1st", @"second": @"2nd", @"third": @"3rd"};
    NSMutableArray *mutableArray = [@[@"a", @"b", @"c"] mutableCopy];
    NSMutableDictionary *mutableDict = [@{@"x": @"X", @"y": @"Y"} mutableCopy];
    
    // 数组下标访问
    NSString *firstItem = array[0];
    NSString *lastItem = array[array.count - 1];
    
    // 字典下标访问
    NSString *firstValue = dict[@"first"];
    NSString *missingValue = dict[@"missing"]; // nil
    
    // 可变容器下标赋值
    mutableArray[0] = @"modified_a";
    mutableArray[mutableArray.count - 1] = @"modified_c";
    
    mutableDict[@"x"] = @"Modified_X";
    mutableDict[@"z"] = @"New_Z";
    
    NSLog(@"Subscript access: %@, %@, %@, %@", firstItem, lastItem, firstValue, missingValue);
    NSLog(@"Modified containers: %@, %@", mutableArray, mutableDict);
}

- (NSArray *)createTestArray {
    return @[
        @"First Element",
        @42,
        @YES,
        @3.14159,
        @{@"nested": @"dictionary"},
        @[@"nested", @"array"]
    ];
}

- (NSDictionary *)createTestDictionary {
    return @{
        @"string_key": @"String Value",
        @"number_key": @100,
        @"bool_key": @NO,
        @"array_key": @[@"elem1", @"elem2", @"elem3"],
        @"dict_key": @{
            @"inner_string": @"Inner Value",
            @"inner_number": @50
        }
    };
}

- (NSNumber *)createTestNumbers {
    // 各种数字字面量组合
    NSNumber *result = @(42 + 8 * 3 - 10 / 2);
    return result;
}

- (void)demonstrateContainerOperations {
    // 字面量在容器操作中的使用
    NSMutableArray *workingArray = [@[@1, @2, @3] mutableCopy];
    
    // 添加字面量元素
    [workingArray addObject:@4];
    [workingArray addObjectsFromArray:@[@5, @6, @7]];
    
    // 修复: 避免隐式布尔转换和无法到达的代码
    NSPredicate *predicate = [NSPredicate predicateWithBlock:^BOOL(NSNumber *number, NSDictionary *bindings) {
        return [number intValue] > 3;  // 修复: 明确类型转换
    }];
    
    NSArray *filtered = [workingArray filteredArrayUsingPredicate:predicate];
    
    NSLog(@"Container operations: original=%@, filtered=%@", workingArray, filtered);
}

- (void)demonstrateNestedLiterals {
    // 复杂嵌套字面量结构
    NSDictionary *complexStructure = @{
        @"users": @[
            @{
                @"id": @1,
                @"name": @"Alice",
                @"active": @YES,
                @"scores": @[@85, @92, @78],
                @"metadata": @{
                    @"created": @"2024-01-01",
                    @"updated": @"2024-01-15"
                }
            },
            @{
                @"id": @2,
                @"name": @"Bob",
                @"active": @NO,
                @"scores": @[@76, @88, @94],
                @"metadata": @{
                    @"created": @"2024-01-02",
                    @"updated": @"2024-01-10"
                }
            }
        ],
        @"settings": @{
            @"version": @"1.0",
            @"features": @[@"feature1", @"feature2", @"feature3"],
            @"limits": @{
                @"max_users": @100,
                @"max_storage": @1024
            }
        }
    };
    
    NSLog(@"Complex nested literals: %@", complexStructure);
}

@end
